<template>
  <div class="geocoding-example">
    <el-card class="demo-card" shadow="hover">
      <template #header>
        <h2>🗺️ 地理编码示例</h2>
      </template>
      
      <div class="demo-section">
        <h3>逆地理编码（坐标转地址）</h3>
        <div class="input-group">
          <el-input-number 
            v-model="testLng" 
            :precision="6" 
            placeholder="经度"
            style="width: 200px;"
          />
          <el-input-number 
            v-model="testLat" 
            :precision="6" 
            placeholder="纬度"
            style="width: 200px;"
          />
          <el-button 
            type="primary" 
            @click="convertToAddress"
            :loading="converting"
          >
            转换为地址
          </el-button>
        </div>
        
        <div v-if="resultAddress" class="result">
          <el-alert 
            :title="resultAddress" 
            type="success" 
            :closable="false"
            show-icon
          />
        </div>
      </div>
      
      <el-divider />
      
      <div class="demo-section">
        <h3>正地理编码（地址转坐标）</h3>
        <div class="input-group">
          <el-input 
            v-model="testAddress" 
            placeholder="请输入地址，如：北京市朝阳区建国门外大街"
            style="width: 400px;"
            @keyup.enter="convertToCoordinates"
          />
          <el-button 
            type="primary" 
            @click="convertToCoordinates"
            :loading="convertingAddr"
          >
            转换为坐标
          </el-button>
        </div>
        
        <div v-if="resultCoordinates" class="result">
          <el-alert 
            :title="`经度: ${resultCoordinates[0]}, 纬度: ${resultCoordinates[1]}`" 
            type="success" 
            :closable="false"
            show-icon
          />
        </div>
      </div>
      
      <el-divider />
      
      <div class="demo-section">
        <h3>预设示例</h3>
        <div class="examples">
          <el-button 
            v-for="example in examples" 
            :key="example.name"
            @click="useExample(example)"
            size="small"
            type="info"
          >
            {{ example.name }}
          </el-button>
        </div>
      </div>
      
      <el-divider />
      
      <div class="demo-section">
        <h3>批量转换示例</h3>
        <el-button 
          type="warning" 
          @click="batchConvert"
          :loading="batchConverting"
        >
          批量转换示例坐标
        </el-button>
        
        <div v-if="batchResults.length > 0" class="batch-results">
          <el-table :data="batchResults" style="width: 100%; margin-top: 16px;">
            <el-table-column prop="coordinates" label="坐标" width="200" />
            <el-table-column prop="address" label="地址" />
          </el-table>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { 
  reverseGeocode, 
  geocode, 
  batchReverseGeocode, 
  formatCoordinates,
  CITY_COORDINATES 
} from '@/utils/geocoding'

// 响应式数据
const testLng = ref(116.995900)
const testLat = ref(26.811509)
const testAddress = ref('福建省莆田市城厢区')
const converting = ref(false)
const convertingAddr = ref(false)
const batchConverting = ref(false)
const resultAddress = ref('')
const resultCoordinates = ref<[number, number] | null>(null)
const batchResults = ref<Array<{coordinates: string, address: string}>>([])

// 预设示例
const examples = [
  { name: '天安门广场', lng: 116.3973, lat: 39.9093 },
  { name: '上海外滩', lng: 121.4907, lat: 31.2408 },
  { name: '莆田示例', lng: 116.995900, lat: 26.811509 },
  { name: '深圳平安大厦', lng: 114.0579, lat: 22.5431 },
  { name: '杭州西湖', lng: 120.1551, lat: 30.2741 }
]

// 方法
const convertToAddress = async () => {
  if (!testLng.value || !testLat.value) {
    ElMessage.warning('请输入有效的经纬度坐标')
    return
  }
  
  converting.value = true
  try {
    const address = await reverseGeocode(testLng.value, testLat.value)
    resultAddress.value = address
    ElMessage.success('转换成功')
  } catch (error) {
    ElMessage.error('转换失败')
    console.error(error)
  } finally {
    converting.value = false
  }
}

const convertToCoordinates = async () => {
  if (!testAddress.value.trim()) {
    ElMessage.warning('请输入地址')
    return
  }
  
  convertingAddr.value = true
  try {
    const coordinates = await geocode(testAddress.value)
    if (coordinates) {
      resultCoordinates.value = coordinates
      ElMessage.success('转换成功')
    } else {
      ElMessage.warning('未找到该地址的坐标')
    }
  } catch (error) {
    ElMessage.error('转换失败')
    console.error(error)
  } finally {
    convertingAddr.value = false
  }
}

const useExample = (example: any) => {
  testLng.value = example.lng
  testLat.value = example.lat
  resultAddress.value = ''
}

const batchConvert = async () => {
  batchConverting.value = true
  try {
    const coordinates: [number, number][] = [
      [116.3973, 39.9093], // 天安门
      [121.4907, 31.2408], // 上海外滩
      [116.995900, 26.811509], // 莆田示例
      [114.0579, 22.5431], // 深圳
      [120.1551, 30.2741]  // 杭州
    ]
    
    const addresses = await batchReverseGeocode(coordinates)
    
    batchResults.value = coordinates.map((coord, index) => ({
      coordinates: formatCoordinates(coord[0], coord[1]),
      address: addresses[index] || '转换失败'
    }))
    
    ElMessage.success('批量转换完成')
  } catch (error) {
    ElMessage.error('批量转换失败')
    console.error(error)
  } finally {
    batchConverting.value = false
  }
}
</script>

<style scoped>
.geocoding-example {
  padding: 20px;
  max-width: 1000px;
  margin: 0 auto;
}

.demo-card {
  border-radius: 12px;
}

.demo-section {
  margin-bottom: 24px;
}

.demo-section h3 {
  margin-bottom: 16px;
  color: #2c3e50;
  font-size: 18px;
}

.input-group {
  display: flex;
  gap: 12px;
  align-items: center;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.result {
  margin-top: 16px;
}

.examples {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.batch-results {
  margin-top: 16px;
}

@media (max-width: 768px) {
  .input-group {
    flex-direction: column;
    align-items: stretch;
  }
  
  .examples {
    flex-direction: column;
  }
}
</style> 